var FG;
if (!FG)
    FG = {};

FG.FeaturePropertyGroup = DX.BBModel.extend({
    tagField: "name",
    urlRoot: "/api/fg/feature_property_group"
});

FG.FeaturePropertyGroupList = DX.BBCollection.extend({
    model: FG.FeaturePropertyGroup,
    url: "/api/fg/feature_property_group"
});

FG.PolygonMap = Backbone.Model.extend({
    urlRoot: '/api/fg/polygon_map'
});

FG.MapPool = function() {
    var this_ = this;
    this.pool = {};

    this.get = function(uri) {
        if (_.has(this_.pool, uri)) {
            return this_.pool[uri];
        } else {
            // TODO: get with async
            return null;
        }
    };

    this.getDfd = function(uri) {
        var dfd = $.Deferred();
        if (_.has(this_.pool, uri)) {
            dfd.resolve(this_.pool[uri]);
        } else {
            var parseRes = this_.parseUri(uri);
            if (!parseRes) {
                dfd.resolve(null);
            } else {
                var mapModel = new parseRes.mapClass({
                    id: parseRes.trueUri
                });
                mapModel.fetch({
                    success: function(model) {
                        this_.set(uri, model);
                        dfd.resolve(model);
                        if (!model.get('public') && !!model.get('index')) {
                            DX.shout('user_index_map:add!', model.attributes);
                        }
                    },
                    error: function(model) {
                        dfd.resolve(null);
                    }
                })
            }

        }
        return dfd;
    };

    this.set = function(uri, model) {
        this.pool[uri] = model;
    };

    this.parseUri = function(uri) {
        if (!uri) {
            return null;
        }
        var mapClass, trueUri, matchRes;
        matchRes = uri.match(/^com:(.*)/);
        mapClass = FG.PolygonMap;
        // TODO: no need match later.
        if (!!matchRes) {
            trueUri = matchRes[1];
        } else {
            trueUri = uri;
        }
        return {mapClass: mapClass, trueUri: trueUri}
    };
};

FG.mapPool = new FG.MapPool();

FG.MapDataManager = function() {
    var this_ = this;
    
    this.moduleInfo = {
        name: 'mapdata',
        zhName: '地图数据',
        desc: '',
        exportList: ['getPropertiesOfGroup']
    };
    
    this.featurePropertyGroupList = new FG.FeaturePropertyGroupList();
    
};

FG.MapDataManager.prototype.loadFeaturePropertyGroups = function() {
    DX.shout('debug!', 'Load FPGroups');
    this.featurePropertyGroupList.fetch();  
};

FG.MapDataManager.prototype.getPropertiesOfGroup = function(groupName) {
    var fpg = this.featurePropertyGroupList.getByTag(groupName);  
    if (!!fpg) {
        return _.clone(fpg.get('properties'))
    } else {
        return {}
    }
};

FG.MapDataManager.prototype.connectSignals = function() {
    var this_ = this,
        app = this.app;
};

DX.on('app:loaded', function(app) {
    var mapDataManager = new FG.MapDataManager();
    mapDataManager.app = app;
    mapDataManager.connectSignals();
    mapDataManager.loadFeaturePropertyGroups();
    app.registerModule(mapDataManager);
});